/*************************************************************************
 * This file is part of HeroesForge.
 *
 * HeroesForge is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * HeroesForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HeroesForge.  If not, see <http://www.gnu.org/licenses/>.
 *************************************************************************/
package org.diskordia.heroesforge.console;

import org.diskordia.heroesforge.client.IClientContext;
import org.diskordia.heroesforge.console.threading.AuthenticateTask;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

/**
 * Presenter of the main screen.
 * 
 * @author lehmamic
 */
@Singleton
public class MainPresenter implements IMainPresenter
{
	private IMainView mainView;
	private IClientContext clientContext;
	
	private Provider<AuthenticateTask> authenticationTaskProvider;
	
	/**
	 * Initializes an instance of the @see MainPresenter class.
	 *
	 * @param view
	 */
	@Inject
	public MainPresenter(IMainView view, IClientContext context, Provider<AuthenticateTask> authenticationTaskInjectionProvider)
	{
		if(view == null)
		{
			throw new IllegalArgumentException("The view instance is a null reference.");
		}
		
		if(context == null)
		{
			throw new IllegalArgumentException("The context instance is a null reference.");
		}
		
		if(authenticationTaskInjectionProvider == null)
		{
			throw new IllegalArgumentException("The authenticationTaskInjectionProvider instance is a null reference.");
		}
		
		this.mainView = view;
		this.clientContext = context;
		
		this.authenticationTaskProvider = authenticationTaskInjectionProvider;
		
		this.ConnectOnStartup();
	}

	/**
	 * @return the mainView
	 */
	public IMainView getMainView() {
		return mainView;
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.console.commands.IMainReceiver#ShowConnectDialog()
	 */
	@Override
	public void ShowConnectDialog() {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * Checks if connect on startup is configured and starts the connection background worker if yes.
	 */
	private void ConnectOnStartup()
	{
		if(this.clientContext.getPreferences().getServerConnection().isAutoConnect())
		{
			AuthenticateTask task = this.authenticationTaskProvider.get();
			task.execute();
		}
	}
}
